<?PHP

/*
Plugin Name: inlineRSS
Plugin URI: http://www.iconophobia.com/
Description: Allows inline RSS feeds from any source in any format (hopefully).
Version: 1.2
Author: Cal Demaine, Jan Wessely
Author URI: http://www.iconophobia.com/
*/

// This is a generic PHP function to pull an RSS feed and return an HTML-formatted string
// The $name is the casual name used in the inlineRSS.txt configuration file, not the
// actual feed URL.  Because HTML is not processed, this can also grab raw HTML files.
// Requests are buffered for a user defined number of minutes on the server, to avoid pounding
// the snot out of the RSS feed source.  The default is 30 minutes.

// Version 0.9  Adds PHP 5, DOM XML, and CURL support
// Version 0.91 Adds debug mode, user-specified XSLT files, Hail-Mary file reads when feed fails
// Version 0.92 Fixes the bugs I added in 0.91, particularly around new feeds
// Version 0.93 Fixes PHP xslt sablotron support, broken in 0.9
// Version 0.94 Checks for xslt-specific function of DOMXML, since some users have DOM without
//              xslt
// Version 0.95 Added a few more DIEs for status reporting, fixed comment processing on blank
//              lines.  Checks to see if it can write to a file before doing it.
// Version 1.0  Fixed get_file_contents branch, added "skipxslt" and "eval" functions, although
//              it's unclear what you'd use these for.
// Version 1.1  Added a second function to call inlineRSS with all variables, with the hopes of
//              being able to provide a better user interface experience.  Yeah, like we care
//              about the users. 
// Version 1.2  Adapted to DokuWiki

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_inlinerss extends DokuWiki_Syntax_Plugin {
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Jan Wessely',
            'email'  => 'info@jawe.net',
            'date'   => '2007-02-28',
            'name'   => 'inlineRSS plugin (syntax plugin)',
            'desc'   => 'Allows inline RSS feeds from any source in any format',
            'url'    => 'http://jawe.net/wiki/proj/dokuwiki/plugins/inlinerss',
        );
    }

    function getType(){ return 'substition'; }
	function getPType(){ return 'block';}
    function getSort(){ return 340; }
    
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern("\\!inlineRSS\\:\\w+", $mode, 'plugin_inlinerss');
    }
    
    function handle($match, $state, $pos, &$handler){
		$data = array();
		if ($state == DOKU_LEXER_SPECIAL) {
			$data['name'] = substr($match, strlen('!inlineRSS:'));
		}
		return $data;
	}
    
	function render($mode, &$renderer, $data) {
		global $ID;
		if ($mode == 'metadata') {
			$renderer->meta['relation']['haspart'][$data['name']] = true;
			//error_log(__FILE__ . ":" . __LINE__ . ": metadata=" . print_r($renderer->meta, true));
		} else if ($mode == 'xhtml') {
			//error_log(__FILE__ . ":" . __LINE__ . ": render mode $mode");
			$inlinerss =& plugin_load('helper', 'inlinerss');
			$renderer->doc .= $inlinerss->parse($data['name']);
		}
	}

} // class

// This is for the php template-calling support:

function tpl_inlineRSS($name,$url = "", $timeout = 30, $translator = "") {
  $inlinerss =& plugin_load('helper', 'inlinerss');
  echo($inlinerss->parse($name, $url, $timeout, $translator));
}

?>
